package com.situ.orderingsystem.dao.impl;

import com.situ.orderingsystem.dao.KindDao;
import com.situ.orderingsystem.global.Global;
import com.situ.orderingsystem.model.Employee;
import com.situ.orderingsystem.model.Kind;
import com.situ.orderingsystem.model.KindSearchBean;
import com.situ.orderingsystem.utils.PaginateInfo;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

public class KindDaoImpl implements KindDao {
    private  final JdbcTemplate template = new JdbcTemplate(Global.getDataSource());

    @Override
    public List<Kind> findAll(KindSearchBean es, PaginateInfo pi) {
        String countsql ="select count(0) from kind";
        String sql = "select id,kind_id,`name`,description from kind";
        List<Object> objectList = new ArrayList<>();

        StringBuilder whysql = new StringBuilder();
        if(es != null){
            whysql.append(" where 1 = 1 and is_deleted = 0");
            if(StringUtils.hasText(es.getKindId())){
                whysql.append(" and kind_id = ?");
                objectList.add(es.getKindId());
            }
            if(StringUtils.hasText(es.getName())){
                whysql.append(" and name like ?");
                objectList.add("%"+es.getName()+"%");
            }
            if(StringUtils.hasText(es.getDescription())){
                whysql.append(" and description like ?");
                objectList.add("%"+es.getDescription()+"%");
            }
        }
        countsql += whysql.toString();
        Long count = template.queryForObject(countsql,Long.class,objectList.toArray());
        pi.setCount(count);
        whysql.append(" limit ?, ?");
        objectList.add(pi.getOffset());
        objectList.add(pi.getLimit());
        sql+=whysql;
        List<Kind> list = template.query(sql,new BeanPropertyRowMapper<>(Kind.class),objectList.toArray());
        return list;
    } public List<Kind> findAll() {

        String sql = "select id,kind_id,`name`,description from kind";
        List<Kind> list = template.query(sql,new BeanPropertyRowMapper<>(Kind.class));
        return list;
    }

    @Override
    public List<Kind> findCombo() {
        String sql = "select kind_id,`name` from kind";
        List<Kind> list = template.query(sql,new BeanPropertyRowMapper<>(Kind.class));
        return list;
    }

    @Override
    public boolean deleteById(Integer id) {
        String sql ="update kind set is_deleted = 1 where id = ?";
        int rows = template.update(sql,id);
        return  rows>0;
    }

    @Override
    public int add(Kind kind) {
        String sql = "insert into kind" +
                "(kind_id,`name`,description)" +
                "values (?,?,?)";
        int rows = template.update(sql,kind.getKindId(),kind.getName(),kind.getDescription());
        return rows;
    }

    @Override
    public Kind searchById(Integer id) {
        String sql = "select kind_id,`name`,description from kind where id = ?";
        List<Kind> emp = template.query(sql,new BeanPropertyRowMapper<>(Kind.class),id);
        return emp.get(0);

    }

    @Override
    public int update(Kind kind) {
        String sql ="update kind set kind_id = ? , `name` = ?  ,description = ? where id = ?";
        int rows = template.update(sql,kind.getKindId(),kind.getName(),kind.getDescription(),kind.getId());
        return  rows ;
    }
}
